﻿Public Class NotaCreditoDAO
    Public Shared Function insertarNotaCredito(ByVal PresentacionId As Integer, ByVal TipoNotaCreditoId As Integer, ByVal TipoEnvioNotaCreditoId As Integer, ByVal NotaCreditoImporte As Double, ByVal NotaCreditoCodigo As Integer, ByVal NotaCreditoPath As String, ByVal NotaCreditoFecha As Date) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_NotaCredito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@PresentacionId", PresentacionId).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoId", TipoNotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@TipoEnvioNotaCreditoId", TipoEnvioNotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoImporte", NotaCreditoImporte).DbType = DbType.Double
                    .AddWithValue("@NotaCreditoCodigo", NotaCreditoCodigo).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoFecha", NotaCreditoFecha).DbType = DbType.Date
                    .AddWithValue("@NotaCreditoPath", NotaCreditoPath).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerNotasCredito(ByVal NotaCreditoId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_NotaCredito_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@NotaCreditoId", NotaCreditoId).DbType = DbType.Int32
            da.Fill(ds, "NotasCredito")

            Return ds.Tables("NotasCredito")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerNotasCreditoXFiltro(ByVal ObraSocialXPlanId As Integer, ByVal Mes As Integer, ByVal Ano As Integer, ByVal FarmaciaId As Integer, ByVal TipoNotaCreditoId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_NotaCredito_obtenerXFiltro", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@Mes", Mes).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@Ano", Ano).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@TipoNotaCreditoId", TipoNotaCreditoId).DbType = DbType.Int32
            da.Fill(ds, "NotasCredito")

            Return ds.Tables("NotasCredito")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarNotaCredito(ByVal NotaCreditoId As Integer, ByVal PresentacionId As Integer, ByVal TipoNotaCreditoId As Integer, ByVal TipoEnvioNotaCreditoId As Integer, ByVal NotaCreditoImporte As Double, ByVal NotaCreditoCodigo As Integer, ByVal NotaCreditoPath As String, ByVal NotaCreditoFecha As Date) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_NotaCredito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoId", NotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@PresentacionId", PresentacionId).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoId", TipoNotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@TipoEnvioNotaCreditoId", TipoEnvioNotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoImporte", NotaCreditoImporte).DbType = DbType.Double
                    .AddWithValue("@NotaCreditoCodigo", NotaCreditoCodigo).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoFecha", NotaCreditoFecha).DbType = DbType.Date
                    .AddWithValue("@NotaCreditoPath", NotaCreditoPath).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarNotaCredito(ByVal NotaCreditoId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_NotaCredito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoId", NotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@PresentacionId", NotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@TipoEnvioNotaCreditoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoImporte", Constante.MENOSUNO).DbType = DbType.Double
                    .AddWithValue("@NotaCreditoCodigo", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@NotaCreditoFecha", Constante.FECHAVACIA).DbType = DbType.Date
                    .AddWithValue("@NotaCreditoPath", Constante.FECHAVACIA).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerNotasCreditoXPresentacion(ByVal PresentacionId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_NotaCredito_obtenerXPresentacion", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@PresentacionId", PresentacionId).DbType = DbType.Int32            
            da.Fill(ds, "NotasCredito")

            Return ds.Tables("NotasCredito")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class

